package tcu.se.SpringAOP;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class JDKproxy {
    public static void main(String[] args) {
        Class[] interfaces = {UserDao.class};
        UserDaoImpl userDao = new UserDaoImpl();

        UserDao dao = (UserDao) Proxy.newProxyInstance(JDKproxy.class.getClassLoader(),interfaces,new UserDaoProxy(userDao));
        int result = dao.add(5,6);
        System.out.println(result);
        String newId = dao.update("苏超");
        System.out.println(newId);
    }
}
class UserDaoProxy implements InvocationHandler{
    //  传入之前的对象
    private Object obj;
    public UserDaoProxy(Object obj){
        this.obj = obj;
    }
    //增强的方法
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("在方法之前执行："+method.getName()+"传递的参数为："+ Arrays.toString(args));
        Object res = method.invoke(obj,args);
        System.out.println("在方法之后执行："+obj);
        return res;
    }
}